﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace AutoPublishWebSite
{
    static class Util
    {
        public static Dictionary<string, string> ParseQueryString(string url)
        {
            if (string.IsNullOrWhiteSpace(url))
            {
                throw new ArgumentNullException("url");
            }
            var uri = new Uri(url);
            if (string.IsNullOrWhiteSpace(uri.Query))
            {
                return new Dictionary<string, string>();
            }
            //1.去除第一个前导?字符
            var dic = uri.Query.Substring(1)
                    //2.通过&划分各个参数
                    .Split(new char[] { '&' }, StringSplitOptions.RemoveEmptyEntries)
                    //3.通过=划分参数key和value,且保证只分割第一个=字符
                    .Select(param => param.Split(new char[] { '=' }, 2, StringSplitOptions.RemoveEmptyEntries))
                    //4.通过相同的参数key进行分组
                    .GroupBy(part => part[0], part => part.Length > 1 ? HttpUtility.UrlDecode(part[1]) : string.Empty)
                    //5.将相同key的value以,拼接
                    .ToDictionary(group => group.Key, group => string.Join(",", group));

            return dic;
        }

        /// <summary>
        /// 获取所有文件
        /// </summary>
        /// <param name="rooPath"></param>
        /// <returns></returns>
        public static List<string> GetAllFiles(string rooPath)
        {
            List<string> list = new List<string>();

            var allDirs = GetDirectory(rooPath);

            foreach (var dir in allDirs)
            {
                var files = new DirectoryInfo(dir).GetFiles();

                foreach (var el in files)
                {
                    list.Add(el.FullName);
                }
            }
            return list;
        }

        //获取目录下的所有文件夹
        public static List<string> GetDirectory(string rootPath, List<string> allDirs = null)
        {
            if (allDirs == null) allDirs = new List<string>() { rootPath };
            var dirs = new DirectoryInfo(rootPath).GetDirectories();
            foreach (var dir in dirs)
            {
                allDirs.Add(dir.FullName);
                GetDirectory(dir.FullName, allDirs);
            }
            return allDirs;
        }

        // 计算SHA1值
        public static string GetSHA1(string filePath)
        {
            using (var algorithm = HashAlgorithm.Create("SHA1"))
            using (var stream = File.OpenRead(filePath))
            using (var bufferedStream = new BufferedStream(stream, 1024 * 1024))
            {
                var hash = algorithm.ComputeHash(bufferedStream);
                var text = String.Join("", hash.Select(x => x.ToString("X2")));
                return text;
            }
        }

        //获取所有文件的sha1
        public static Dictionary<string, string> GetAllFilesSHA1(string rooPath)
        {
            Dictionary<string, string> dict = new Dictionary<string, string>();

            var allDirs = GetDirectory(rooPath);

            foreach (var dir in allDirs)
            {
                var files = new DirectoryInfo(dir).GetFiles();

                foreach (var el in files)
                {
                    dict.Add(el.FullName, GetSHA1(el.FullName));
                }
            }
            return dict;
        }

    }
}
